package com.freemyleft.left.zapp.selectimage.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;


import com.freemyleft.left.R;
import com.freemyleft.left.zapp.selectimage.FileUtils;
import com.freemyleft.left.zapp.selectimage.PicassoUtils;
import com.freemyleft.left.zapp.selectimage.bean.Image;
import com.freemyleft.left.zapp.selectimage.clipimage.ClipImageLayout;
import com.freemyleft.left.zapp.selectimage.listener.CameraListener;


public class ImageEditActivity extends FragmentActivity {
    private Image mImageBean;
    private ClipImageLayout mImage;
    private TextView mFinish;
    private static CameraListener mCameraListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_edit);
        if (CameraActivity.mActivity != null){
            CameraActivity.mActivity.finish();
        }
        initLayout();
        initView();
    }

    public static void setOnCameraListener(CameraListener cameraListener){
        mCameraListener = cameraListener;
    }

    private void initLayout() {
        mImage = (ClipImageLayout) findViewById(R.id.m_image);
        mFinish = (TextView) findViewById(R.id.m_finish);
        mFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = mImage.clip();
                String imageName = String.valueOf(System.currentTimeMillis()) + ".jpg";
                String path = FileUtils.saveNativePath(bitmap, imageName);
                Image image = FileUtils.getImage(ImageEditActivity.this, path, imageName);
                if (mCameraListener != null){
                    mCameraListener.cameraListener(image);
                    finish();
                }
            }
        });
    }

    private void initView() {
        mImageBean = (Image) getIntent().getSerializableExtra("bean");
        if (mImageBean == null){
            new RuntimeException("mImageBean == null");
            finish();
            return;
        }
//        Bitmap bitmap= BitmapFactory.decodeFile("/storage/emulated/0/Pictures/Screenshots/Screenshot_2017-06-13-23-29-48.png");
//        mImage.getImage().setImageBitmap(bitmap);
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        PicassoUtils.picasso(ImageEditActivity.this,mImageBean.path,
                height, width,  mImage.getImage());
    }

}
